<?php

class UserFeedsTb extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $feed_title;

    /**
     *
     * @var string
     */
    public $feed_container;

    /**
     *
     * @var string
     */
    public $feed_type;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var integer
     */
    public $sender_id;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     *
     * @var string
     */
    public $date_modified;
    public $receivers;
    public $params;

    public function listProfileFeedsById($feed_id)
    {
        $phql = "SELECT uft.*,uat.* FROM UserFeedsTb uft 
                LEFT JOIN UserAccountTb uat on uat.id = uft.sender_id
                WHERE uft.id = ?0
                ORDER BY uft.date_created DESC,uft.date_modified DESC LIMIT 5";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$feed_id));
        return $data;
    }
    public function listProfileFeeds($user_id)
    {
        $phql = "SELECT uft.*,uat.* FROM UserFeedsTb uft 
                LEFT JOIN UserAccountTb uat on uat.id = uft.sender_id
                WHERE uft.user_id = ?0
                ORDER BY uft.date_created DESC,uft.date_modified DESC LIMIT 5";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data;
    }

    public function listFeeds($user_id)
    {
        $phql = "SELECT uft.*,uat.*,uatt.* FROM UserFeedsTb uft 
                LEFT JOIN UserAccountTb uat on uat.id = uft.sender_id 
                LEFT JOIN UserAccountTb uatt on uat.id = uft.user_id 
                LEFT JOIN UserConnectionsTb uct on uct.friend_id = uft.sender_id
                WHERE uct.user_id = ?0 or uft.user_id = ?0 or uft.feed_type = 'events'
                GROUP BY uft.id
                ORDER BY uft.date_created DESC,uft.date_modified DESC";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data;   
    }


    public function beforeValidationOnCreate()
    {
        $this->date_created = CURR_DATE;

    }
    public function beforeValidationOnUpdate()
    {
        $this->date_modified = CURR_DATE;
    }
    /**
     * Independent Column Mapping.
     */
    public function countPosts($user_id)
    {
        $phql = "SELECT COUNT(id) AS TOTAL FROM UserFeedsTb
                WHERE sender_id = ?0";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data[0]->TOTAL;
    }
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'feed_title' => 'feed_title', 
            'feed_container' => 'feed_container', 
            'feed_type' => 'feed_type', 
            'user_id' => 'user_id', 
            'sender_id' => 'sender_id', 
            'date_created' => 'date_created', 
            'date_modified' => 'date_modified',
            'receivers' => 'receivers',
            'params' => 'params'
        );
    }


}
